#include <REGX52.H>
#include "LCD1602.h"
#include "DS1302.h"
#include "Delay.h"
#include "Timer0.h"
#include "Key.h"

//按键，模式，设置选择位
unsigned char KeyNum,MODE=0,TimeSetSelect=0,TimeSetFlash;

void TimeShow()
{	
		DS1302_ReadTime();//将时间保存到DS1302_Time数组
		LCD_ShowNum(1,1,DS1302_Time[0],2);
		LCD_ShowNum(1,4,DS1302_Time[1],2);
		LCD_ShowNum(1,7,DS1302_Time[2],2);
		LCD_ShowNum(1,14,DS1302_Time[6],1);
		
		LCD_ShowNum(2,1,DS1302_Time[3],2);
		LCD_ShowNum(2,4,DS1302_Time[4],2);
		LCD_ShowNum(2,7,DS1302_Time[5],2);
}

void TimeSet()
{
	if(KeyNum==2) //切换选择位
	{
		TimeSetSelect++;
//	if(TimeSetSelect>6){TimeSetSelect=0;}
		TimeSetSelect%=7; //与上述等同
	}
	else if(KeyNum==3)
	{
		DS1302_Time[TimeSetSelect]++;
		if(DS1302_Time[0]>99){DS1302_Time[0]=0;}
		if(DS1302_Time[1]>12){DS1302_Time[1]=1;}
		
		if(DS1302_Time[1]==1||DS1302_Time[1]==3||DS1302_Time[1]==5||
			 DS1302_Time[1]==7||DS1302_Time[1]==8||DS1302_Time[1]==10||
		   DS1302_Time[1]==12)
		{
			if(DS1302_Time[2]>31){DS1302_Time[2]=1;}		
		}
		else if(DS1302_Time[1]==4||DS1302_Time[1]==6||DS1302_Time[1]==9||
			 DS1302_Time[1]==11)
		{
			if(DS1302_Time[2]>30){DS1302_Time[2]=1;}		
		}
		else if(DS1302_Time[1]==2)
		{
			if(DS1302_Time[0]%4==0)
			{
				if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
			}
			else
			{
				if(DS1302_Time[2]>28){DS1302_Time[2]=1;}
			}				
		}
		
		if(DS1302_Time[3]>23){DS1302_Time[3]=0;}
		if(DS1302_Time[4]>59){DS1302_Time[4]=0;}
		if(DS1302_Time[5]>59){DS1302_Time[5]=0;}	
		if(DS1302_Time[6]>7){DS1302_Time[6]=1;}		
	}
	else if(KeyNum==4)
	{
		DS1302_Time[TimeSetSelect]--;
		if(DS1302_Time[0]<0){DS1302_Time[0]=99;}
		if(DS1302_Time[1]<1){DS1302_Time[1]=12;}
		
		if(DS1302_Time[1]==1||DS1302_Time[1]==3||DS1302_Time[1]==5||
			 DS1302_Time[1]==7||DS1302_Time[1]==8||DS1302_Time[1]==10||
		   DS1302_Time[1]==12)
		{
			if(DS1302_Time[2]<1){DS1302_Time[2]=31;}	
			if(DS1302_Time[2]>31){DS1302_Time[2]=1;}			
		}
		else if(DS1302_Time[1]==4||DS1302_Time[1]==6||DS1302_Time[1]==9||
			 DS1302_Time[1]==11)
		{
			if(DS1302_Time[2]<1){DS1302_Time[2]=30;}
			if(DS1302_Time[2]>30){DS1302_Time[2]=1;}			
		}
		else if(DS1302_Time[1]==2)
		{
			if(DS1302_Time[0]%4==0)
			{
				if(DS1302_Time[2]<1){DS1302_Time[2]=29;}
				if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
			}
			else
			{
				if(DS1302_Time[2]<1){DS1302_Time[2]=28;}
				if(DS1302_Time[2]>28){DS1302_Time[2]=1;}				
			}				
		}
		
		if(DS1302_Time[3]<0){DS1302_Time[3]=23;}
		if(DS1302_Time[4]<0){DS1302_Time[4]=59;}
		if(DS1302_Time[5]<0){DS1302_Time[5]=59;}	
		if(DS1302_Time[6]<1){DS1302_Time[6]=7;}	
	}
	if(TimeSetSelect==0&&TimeSetFlash==0){LCD_ShowString(1,1,"  ");}
	else{LCD_ShowNum(1,1,DS1302_Time[0],2);}
	if(TimeSetSelect==1&&TimeSetFlash==0){LCD_ShowString(1,4,"  ");}
	else{LCD_ShowNum(1,4,DS1302_Time[1],2);}
	if(TimeSetSelect==2&&TimeSetFlash==0){LCD_ShowString(1,7,"  ");}
	else{LCD_ShowNum(1,7,DS1302_Time[2],2);}
	if(TimeSetSelect==6&&TimeSetFlash==0){LCD_ShowString(1,14,"  ");}
	else{LCD_ShowNum(1,14,DS1302_Time[6],2);}	
	if(TimeSetSelect==3&&TimeSetFlash==0){LCD_ShowString(2,1,"  ");}
	else{LCD_ShowNum(2,1,DS1302_Time[3],2);}
	if(TimeSetSelect==4&&TimeSetFlash==0){LCD_ShowString(2,4,"  ");}
	else{LCD_ShowNum(2,4,DS1302_Time[4],2);}
	if(TimeSetSelect==5&&TimeSetFlash==0){LCD_ShowString(2,7,"  ");}
	else{LCD_ShowNum(2,7,DS1302_Time[5],2);}	
}

void main()
{
	LCD_Init();
	DS1302_Init();
	Timer0_Init();
	LCD_ShowString(1,1,"  -  -   DAY:");
	LCD_ShowString(2,1,"  :  :");		
	DS1302_SetTime();//设置默认时间
	while(1)
	{
		KeyNum = Key();
		if(KeyNum==1) //通过按键1进行模式切换
		{
			MODE = 1-MODE;
			TimeSetSelect=0;
			DS1302_SetTime();//切换模式时将时间写入单片机寄存器
		}
		switch(MODE)
		{
			case 0: TimeShow();break;//显示
			case 1: TimeSet();break;//设置
		}
	}
}

void Timer0_Routine() interrupt 1
{
	static unsigned int T0Count;
	TL0 = 0x18;				//设置定时初始值
	TH0 = 0xFC;				//设置定时初始值
	T0Count++;
	if(T0Count>=500)
	{
		T0Count = 0;
		TimeSetFlash =!TimeSetFlash; //逻辑取反
	}
}